//
//  CompanyListViewController.m
//  u76ho
//
//  Created by m-air-01 on 16/1/21.
//  Copyright © 2016年 LV. All rights reserved.
//

#import "CompanyListViewController.h"
#import "RecommendCompanyCell.h"
#import "ChengZhangViewController.h"
#import "CommendCompanyModel.h"
#import "LoginViewController.h"
#import "FeRollingLoader.h"
@interface CompanyListViewController ()
<UITableViewDelegate,UITableViewDataSource,UIAlertViewDelegate>
{
    UITableView * _tableView;
    NSMutableArray * dataSource;
}

@property (strong, nonatomic) FeRollingLoader *rollingLoader;
@end

@implementation CompanyListViewController

#pragma mark - 视图的生命周期
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];

   
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self main];
    
    _rollingLoader = [[FeRollingLoader alloc] initWithView:self.view title:@"LOADING"];
    [self.view addSubview:_rollingLoader];
    [_rollingLoader show];
    self.navigationItem.title = @"推荐企业";
    //[self addBackItem];
    
    // Do any additional setup after loading the view.
}
-(void)main
{
    dataSource = [[NSMutableArray alloc]init];
    self.view.backgroundColor = [UIColor whiteColor];
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 63, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerNib:[UINib nibWithNibName:@"RecommendCompanyCell" bundle:nil] forCellReuseIdentifier:@"RecommendCompanyCell"];
    [self.view addSubview:_tableView];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self requestData];
}
-(void)requestData
{
    [U76HONet commendCompanyListWithAction:@"" Type:_type andDataBlock:^(NSArray *arr) {
        NSLog(@"推荐企业获取数据成功了");
        [dataSource addObjectsFromArray:arr];
        
        [_tableView reloadData];
        [_rollingLoader removeFromSuperview];
    }];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataSource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RecommendCompanyCell * cell = [tableView dequeueReusableCellWithIdentifier:@"RecommendCompanyCell"];
    cell.HomePagecommendModel = [dataSource objectAtIndex:indexPath.row];
    cell.isHiddenZP = @"YES";
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 1;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self isLogin]) {
        CommendCompanyModel * model = dataSource[indexPath.row];
        ChengZhangViewController * companyVC = [[ChengZhangViewController alloc]init];
        companyVC.yqCode = model.code;
       // companyVC.MyTitle = model.companyname;
        [self.navigationController pushViewController:companyVC animated:YES];
    }
}
-(BOOL)isLogin
{
    if ([Singleton share].code) {
        NSLog(@"登录了");
        return YES;
        
    }
    else
    {
        //        NSLog(@"还没登录呢");
        //        LoginViewController * loginVC = [[LoginViewController alloc]init];
        //        [self.navigationController pushViewController:loginVC animated:NO];
        //        return NO;
        if (SYSTEM_VERSION >= 8.0) {
            UIAlertController * alerVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"您还没有登录是否登录" preferredStyle:UIAlertControllerStyleAlert];
            [alerVC addAction:[UIAlertAction actionWithTitle:@"登录" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                
                LoginViewController * loginVC = [[LoginViewController alloc]init];
                UINavigationController *nvc = [[UINavigationController alloc]initWithRootViewController:loginVC];
                nvc.navigationBarHidden = YES;
                
                [self presentViewController:nvc animated:YES completion:nil];
                
            }]];
            [alerVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                
            }]];
            [self.view.window.rootViewController presentViewController:alerVC animated:YES completion:nil];
            
        }
        else
        {
            UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您还没有登录是否登录" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alert show];
        }
        return NO;
        
    }
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    int i = (int)buttonIndex;
    if (i == 0) {
        
    }
    else if(i == 1)
    {
        LoginViewController * loginVC = [[LoginViewController alloc]init];
        UINavigationController *nvc = [[UINavigationController alloc]initWithRootViewController:loginVC];
        nvc.navigationBarHidden = YES;
        
        [self presentViewController:nvc animated:YES completion:nil];
    }
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
